package com.boot.app.file.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.app.file.domain.FileAttach;
import com.boot.app.file.mapper.FileAttachMapper;
import com.boot.app.file.service.IFileAttachService;
import com.boot.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

@Service
public class FileAttachServiceImpl extends ServiceImpl<FileAttachMapper, FileAttach> implements IFileAttachService {

    private final FileAttachMapper fileAttachMapper;

    @Autowired
    public FileAttachServiceImpl(FileAttachMapper fileAttachMapper){
        this.fileAttachMapper = fileAttachMapper;
    }

    @Value("${attach.group.upload.path}")
    private String FILE_PATH;

    private String getUploadPath() throws FileNotFoundException {
        return CommonUtil.getClassPath() + FILE_PATH;
    }

    private String ensureGroupPathExists(long groupId) throws FileNotFoundException {
        String targetDir = getGroupPath(groupId);
        File file = new File(targetDir);
        if(!file.exists()){
            file.mkdirs();
        }
        return targetDir;
    }

    @Override
    public String getGroupPath(long groupId) throws FileNotFoundException {
        return getUploadPath() + groupId + "\\";
    }

    @Transactional
    @Override
    public FileAttach saveAttachByGroupId(MultipartFile file, Long groupId) throws IOException {
        if(file.isEmpty()) {
            return null;
        }
        if(groupId == null){//为空时新增附件组
            groupId = IdWorker.getId();
        }
        ensureGroupPathExists(groupId);

        String originalFilename = file.getOriginalFilename();
        String type = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();

        long attachId = IdWorker.getId();
        String attachName =  attachId + "." + type;

        FileAttach fileAttach = new FileAttach();
        fileAttach.setId(attachId);
        fileAttach.setGroupId(groupId);
        fileAttach.setName(attachName);
        fileAttach.setType(type);
        fileAttachMapper.insert(fileAttach);

        File targetFile = new File(getGroupPath(groupId) + attachName);
        file.transferTo(targetFile);
        return fileAttach;
    }

    @Transactional
    @Override
    public FileAttach removeAttachById(long id) throws FileNotFoundException {
        FileAttach fileAttach = fileAttachMapper.selectById(id);
        fileAttachMapper.deleteById(id);

        File file = new File(getGroupPath(fileAttach.getGroupId()) + fileAttach.getName());
        if(file.exists()){
            file.delete();
        }
        return fileAttach;
    }

}
